MSDN Documentation

.NET Framework Assemblies

OdbcHException Class

Represents an error that occurs when an ODBC driver or the underlying ODBC functions return an error. This exception is typically thrown by the System.Data.Odbc classes when interacting with an ODBC data source.

Inheritance Hierarchy

System.Object
System.Exception
System.Data.Common.DataException
System.Data.Odbc.OdbcException
System.Data.Odbc.OdbcHException

Syntax

public sealed class OdbcHException : OdbcException

Remarks

The OdbcHException class provides information about errors returned by the ODBC API. When an error occurs during an ODBC operation, an OdbcException is thrown. If the error originates from the ODBC driver or a specific ODBC API call that returns an HRESULT, an OdbcHException might be instantiated.

This exception class is sealed, meaning it cannot be inherited. It contains information about the native ODBC error code and message, which can be crucial for diagnosing issues with ODBC data source connectivity or operations.

The OdbcErrorCollection property of the base OdbcException class will contain detailed information about each error returned by the ODBC driver.

Constructors

Modifier Name Description
protected OdbcHException(SerializationInfo info, StreamingContext context) Initializes a new instance of the OdbcHException class with serialized data. (Internal use only)

Properties

Modifier Name Description
public HResult Gets the HRESULT associated with the ODBC error. This property is inherited from System.Exception.
public override Message Gets a message that describes the current exception. This property is inherited from System.Exception.
public NativeError Gets the native ODBC error code.

Methods

Modifier Name Description
protected override GetBaseException() When overridden in a derived class, returns the Exception that is the root cause of the current exception. Inherited from System.Exception.
public override ToString() Overrides the ToString() method to include NativeError and HResult information.

Example

using System;
using System.Data;
using System.Data.Odbc;

public class Example {
    public static void Main() {
        string connectionString = "DSN=MyODBCDataSource;Uid=user;Pwd=password;";

        using (OdbcConnection connection = new OdbcConnection(connectionString)) {
            try {
                connection.Open();
                // Perform database operations here
            } catch (OdbcException odbcEx) {
                Console.WriteLine($"An OdbcException occurred: {odbcEx.Message}");
                foreach (OdbcError error in odbcEx.Errors) {
                    Console.WriteLine($" - NativeError: {error.NativeError}");
                    Console.WriteLine($" - Message: {error.Message}");
                    Console.WriteLine($" - SQLState: {error.SQLState}");
                    Console.WriteLine($" - ErrorCode: {error.ErrorCode}");
                }

                // Check for OdbcHException specifically if needed
                if (odbcEx is OdbcHException hEx) {
                    Console.WriteLine($" - HResult: {hEx.HResult}");
                    Console.WriteLine($" - NativeError (from HEx): {hEx.NativeError}");
                }
            } catch (Exception ex) {
                Console.WriteLine($"An unexpected error occurred: {ex.Message}");
            }
        }
    }
    }

Requirements

Assembly Version
System.Data 1.0.5000.0, 2.0.0.0, 3.0.0.0, 3.5.0.0, 4.0.0.0